/**
 * Created by lincheng on 16/9/12.
 */
if (typeof jQuery === 'undefined') {
  throw new Error('Requires jQuery')
}
function initPlugins(target) {
  if (target == undefined) {
    return;
  }
  if (typeof target == "string") {
    target = $(target);
  }
  if ((typeof target !== "object") || (typeof target.find !== "function")) {
    return;
  }
  //tooltip
  if (typeof jQuery.fn.tooltip != "undefined") {
    target.find('[data-toggle="tooltip"]').tooltip();
  }
  //slimScroll
  if (typeof jQuery.fn.slimScroll != "undefined") {
    target.find('[data-scroll="slim"]').slimScroll({opacity: 0.5, height: '100%', alwaysVisible: true});
  }
  //selectpicker to select
  if (typeof jQuery.fn.selectpicker != "undefined") {
    target.find('[data-select="picker"]').selectpicker();
  }
  //daterangepicker to input
  if (typeof jQuery.fn.daterangepicker != "undefined") {
    target.find('[data-datepicker="range"]').each(function () {
      var $this = $(this);
      var format = $this.attr("data-format") || "YYYY-MM-DD HH:mm";
      var from = $this.attr("data-time-from") && $($this.attr("data-time-from"));
      var to = $this.attr("data-time-to") && $($this.attr("data-time-to"));
      var delta = $this.attr("data-time-delta") && parseInt($this.attr("data-time-delta")) || 90;
      var ranges = $this.attr("data-time-ranges");
      var now = moment().minutes(Math.ceil(moment().minutes() / 5) * 5).seconds(0);
      var start = moment(now).subtract(delta, 'days');
      var end = moment(now);
      if (from.length && (typeof from.val == "function")) {
        var val_from = from.val();
        if (val_from && moment(val_from).isValid()) {
          start = moment(val_from);
        } else {
          from.val(start.format(format));
        }
      }
      if (to.length && (typeof to.val == "function")) {
        var val_to = to.val();
        if (val_to && moment(val_to).isValid()) {
          end = moment(val_to);
        } else {
          to.val(end.format(format));
        }
      }
      var options = {
        locale: {format: format},
        showDropdowns: true,
        autoApply: true,
        timePicker: true,
        timePicker24Hour: true,
        timePickerIncrement: 5,
        startDate: start,
        endDate: end,
        maxDate: now
      };
      if (ranges) {
        options.ranges = {
          '今天': [moment().startOf('day'), moment(now)],
          '本月': [moment(now).startOf('month'), moment(now)],
          '上月': [moment(now).subtract(1, 'months').startOf('month'), moment(now).startOf('month')],
          '最近1周': [moment(now).subtract(7, 'days').startOf('day'), moment(now)],
          '最近2周': [moment(now).subtract(14, 'days').startOf('day'), moment(now)],
          '最近1月': [moment(now).subtract(1, 'months').startOf('day'), moment(now)],
          '最近3月': [moment(now).subtract(3, 'months').startOf('day'), moment(now)],
          '最近1年': [moment(now).subtract(1, 'years').startOf('day'), moment(now)]
        }
      }
      $this.daterangepicker(options, function (start, end, label) {
        $this.html(start.format(format) + " ~ " + end.format(format));
        if (from.length && (typeof from.val == "function")) {
          from.val(start.format(format));
        }
        if (to.length && (typeof to.val == "function")) {
          to.val(end.format(format));
        }
      }).html(start.format(format) + " ~ " + end.format(format));
    });
    target.find('[data-datepicker="single"]').each(function () {
      var $this = $(this);
      var format = $this.attr("data-format") || "YYYY-MM-DD HH:mm";
      var now = moment().minutes(Math.floor(moment().minutes() / 5) * 5);
      var start = now;
      if ($this.val() != "") {
        start = moment($this.val());
      }
      $this.daterangepicker(
        {
          locale: {format: format},
          singleDatePicker: true,
          showDropdowns: true,
          timePicker: true,
          timePicker24Hour: true,
          timePickerIncrement: 5,
          startDate: start,
          maxDate: now
        },
        function (start, end, label) {
          $this.val(start.format(format));
        }
      );
    });
  }
}

'use strict';
$(function ($) {
  initPlugins('body');
});


